#include "portdao.h"
#include<dbutil.h>
#ifdef LINUX_DB
#include <stdio.h>
#else
#include <QSqlQuery>
#include <QVariant>
#include <QSqlError>
#include <QSqlDatabase>
#endif
PortDao::PortDao()
{
}
DevicePort PortDao::getPortDetail(int portId, int deviceId)
{
    DevicePort port;
    QString str("select * from JYDW_DEVICE_PORT where DEVICE_ID=%1 and PORT=%2");
    str = str.arg(DbUtils::getStringFormInt(deviceId), DbUtils::getStringFormInt(portId));
#ifdef LINUX_DB
    DbUtils dbUtils;
    char sql[500];
    sprintf(sql, str.toLocal8Bit().data());
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct port_result), "PortDao::getPortDetail");
    while (result != NULL)
    {
        port_result *p_result = (port_result *)result;
        port.setDeviceId(p_result->deviceId);
        port.setPort(p_result->port);
        port.setInterfaceid(p_result->interfaceId);
        port.setCircuit(p_result->circuit);
        port.setMac(p_result->mac);
        port.setStatus(p_result->status);
        port.setCreateTime(DbUtils::getTimeFromLong(p_result->createTime));
        port.setUpdateTime(DbUtils::getTimeFromLong(p_result->updateTime));
        result = dbUtils.getNextRow();
        break;
    }
    dbUtils.freeRes();
    return port;
#else
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("PortDao::getPortDetail", "SQL:" + str);
    }
    while (query.next())
    {
        port.setDeviceId(query.value(0).toInt());
        port.setPort(query.value(1).toInt());
        port.setInterfaceid(query.value(2).toInt());
        port.setCircuit(query.value(3).toString());
        port.setMac(query.value(4).toString());
        port.setStatus(query.value(5).toInt());
        port.setCreateTime(query.value(6).toDateTime());
        port.setUserId(query.value(7).toString());
        port.setUpdateTime(query.value(8).toDateTime());
        port.setUpdateId(query.value(9).toString());
    }
#endif
    return port;
}

list<DevicePort *> PortDao::getPortsByDeviceId(int deviceId)
{
    list<DevicePort *> result;
    QString str("select * from JYDW_DEVICE_PORT where DEVICE_ID=%1");
    str = str.arg(DbUtils::getStringFormInt(deviceId));
#ifdef LINUX_DB

    DbUtils dbUtils;
    char sql[500];
    sprintf(sql, str.toLocal8Bit().data());
    MYSQL_ROW sqlResult = dbUtils.startQuery(sql, sizeof(struct port_result), "PortDao::getPortsByDeviceId");
    while (sqlResult != NULL)
    {
        port_result *p_result = (port_result *)sqlResult;
        DevicePort *port = new DevicePort;
        port->setDeviceId(p_result->deviceId);
        port->setPort(p_result->port);
        port->setInterfaceid(p_result->interfaceId);
        port->setCircuit(p_result->circuit);
        port->setMac(p_result->mac);
        port->setStatus(p_result->status);
        port->setCreateTime(DbUtils::getTimeFromLong(p_result->createTime));
        port->setUpdateTime(DbUtils::getTimeFromLong(p_result->updateTime));
        result.push_back(port);
        sqlResult = dbUtils.getNextRow();
    }
    dbUtils.freeRes();
    return result;
#else
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("PortDao::getPortsByDeviceId", "SQL:" + str);
    }
    while (query.next())
    {
        DevicePort *port = new DevicePort();
        port->setDeviceId(query.value(0).toInt());
        port->setPort(query.value(1).toInt());
        port->setInterfaceid(query.value(2).toInt());
        port->setCircuit(query.value(3).toString());
        port->setMac(query.value(4).toString());
        port->setStatus(query.value(5).toInt());
        port->setCreateTime(query.value(6).toDateTime());
        port->setUserId(query.value(7).toString());
        port->setUpdateTime(query.value(8).toDateTime());
        port->setUpdateId(query.value(9).toString());
        result.push_back(port);
    }
    return result;
#endif
}
